<template>
  <div class="container">
    <!-- 头部搜索框 -->
    <el-form :inline="true" size="small">
      <el-col :span="16">
        <el-form-item label="关键词">
          <el-input
            v-model="page.keyword"
            placeholder="根据编号搜索"
          />
        </el-form-item>
      </el-col>
      <el-col :span="8" style="text-align: right">
        <el-button size="small" @click="Del">清除</el-button>
        <el-button size="small" type="primary" @click="Search">搜索</el-button>
      </el-col>
    </el-form>
    <!-- 灰色提示框 -->
    <el-alert
      :title="`数据一共${page.total}条`"
      type="info"
      description=""
      show-icon
    />
    <!-- 表格主体 -->
    <el-table :data="list" style="width: 100%" fit>
      <el-table-column width="210" prop="id" label="编号" />
      <el-table-column
        prop="questionType"
        :formatter="formatFn"
        label="题型"
        sortable
      />
      <el-table-column width="210" prop="questionIDs" label="题目编号" sortable>
        <template #default="{ row }">
          <div v-for="(item, index) in row.questionIDs" :key="index">
            <el-link
              :underline="false"
              type="primary"
              @click="preview(item.id)"
            >{{ item.number }}</el-link>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="addTime" label="录入时间" sortable />
      <el-table-column prop="totalSeconds" label="答题时间(s)" sortable />
      <el-table-column prop="accuracyRate" label="正确率(%)" sortable />
      <el-table-column prop="userName" label="录入人" sortable />
      <el-table-column label="操作" fixed="right" sortable>
        <template #default="{ row }">
          <el-button
            type="danger"
            icon="el-icon-delete"
            circle
            size="small"
            plain
            @click="del(row)"
          />
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      @size-change="SizeChange"
      @current-change="CurrentChange"
      :current-page="page.page"
      :page-sizes="[20, 30, 50, 100]"
      :page-size="1"
      layout="sizes, prev, pager, next, jumper"
      :total="page.total"
    />
    <!-- 弹窗详情 -->
    <PreView ref="preView" :show.sync="show" id="id" />
  </div>
  <!-- <div class='container'>主题列表</div> -->
</template>

<script>
import PreView from '../components/preview'
import { randoms, removeRandoms } from '@/api/hmmm/questions.js'
import { questionType } from '@/api/hmmm/constants.js'
export default {
  components: {
    PreView
  },
  data () {
    return {
      page: {
        page: 1,
        pagesize: 20,
        keyword: ''
      },
      list: [],
      show: false,
      id: '', // 正在操作的id信息
      formData: {
        username: ''
      }
    }
  },
  created () {
    this.getData()
  },
  methods: {
    // 获取数据列表
    async getData () {
      const { data } = await randoms(this.page)
      this.list = data.items
      this.page.total = data.counts
    },
    // 清空输入框内容
    Del () {
      this.page.keyword = ''
    },
    // 搜索功能
    Search () {
      this.page.page = 1
      this.getData()
    },
    // 页数设置
    SizeChange (val) {
      this.page.pagesize = val
      if (this.page.page === 1) {
        this.getData(this.page)
      }
    },
    // 页码设置
    CurrentChange (val) {
      this.page.page = val
      this.getData()
    },
    // 枚举题型
    formatFn (row) {
      const res = questionType.find((item) => item.value === +row.questionType)
      return res ? res.label : '未知'
    },
    // 预览功能
    preview (id) {
      this.show = true
      this.$refs.preView.getChoice(id)
    },
    // 删除数据
    del (data) {
      this.$confirm('你确定要删除该数据吗?', '温馨提示').then(async () => {
        await removeRandoms(data)
        this.$message.success('删除数据成功')
        await this.getData()
      })
    }
  }
}
</script>
